var grpCode=getURLPara('grp_code');
_lSSaveOrUpdate( 'grpCode', grpCode );
var cityname='';
var lat='';
var lon='';
var district_s='';
var geolocation = new BMap.Geolocation();
// 创建地理编码实例
var myGeo = new BMap.Geocoder();
geolocation.getCurrentPosition(function(r){
    if(this.getStatus() == BMAP_STATUS_SUCCESS){
        console.log('ppp')
        console.log(r.address.city); //城市
        console.log(r.point.lat) ;//经度
        console.log(r.point.lng); //纬度
        cityname=r.address.city;//北京市变成北京
        //alert(cityname);
        lat=r.point.lat;
        lon=r.point.lng;
        //localStorage.setItem( "lat", r.point.lat );
        //localStorage.setItem( "lon", r.point.lng );
        //localStorage.setItem( "cityname", r.address.city);
        var pt = r.point;
        // 根据坐标得到地址描述
        myGeo.getLocation(pt, function(result){
            if (result){
                var addComp = result.addressComponents;
                //console.log(addComp.district) ;//区
                district_s=addComp.district;
                //localStorage.setItem( "district", addComp.district);
            }
        });
    }
});


var vm =  new Vue({
    el : "#app",
    data : {

        city_chow:  false,
        county_chow:false,
        ctrl       :false,
        imgServerUrl:'',//地址
        city       :'定位中...',//当前城市
        city_or    :'',//城市中间键
        //district:district_s,//当前城市的区
        district   :'',//当前城市的区
        city_lat   :'',//当前城市经度
        city_lon   :'',//
        witch:1,//控制是否检测到城市名变化
        token:'',//id
        page:1,//默认第几页
        tipMsg:'加载中',//加载提示文字
        cityLat:'',//定位经度
        cityLon:'',//定位经度
        province:'',
        city_aggre:[],//城市集合
        county_aggre:[],//当前城市的地区集合
        cont_list :[],//列表集合
        currentPage:1,//初始化第几页
        id:6,
        ctrl_flag:true//控制第一次定位需要请求的状态,否则会多次请求

    },
    created:function(){

        console.log(backUrl)
        this.token = _lSGet('token');
        if( _lSGet('urls')){
            var urls =JSON.parse( _lSGet('urls'));
            this.imgServerUrl=urls.imgServerUrl;
        }else{
            this.imgServerUrl=imgViewUrl;//congif里的图片路径
        };
        //console.log(99999)
        //console.log(this.imgServerUrl);
        //alert(this.imgServerUrl);
        this.get_this_city();//获取定位城市
        console.log('adadad');
        console.log(this.city,this.district);
        this.get_data(this.city,this.district);
        //layer.open({
        //    type: 2
        //    ,content: 'test'
        //    ,time: 0.5
        //});

    },
    mounted:function(){


        var that=this;
        $(window).scroll(function(){//判断页面到底
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = $(this).height();
            if(scrollTop + windowHeight == scrollHeight){
                that.page++;
                that.get_data();
                //alert(that.page);
                //console.log(that.page);
                //console.log(that.hisOrder);
                layer.open({
                    type: 2
                    ,content: that.tipMsg
                    ,time: 0.5
                });
            }
        });
        setTimeout(function(){//10秒定位失败处理
            if(that.witch==1){
                if(that.ctrl_flag==true){
                    this.get_city_addre();
                    this.get_data(this.city,null);
                    that.ctrl_flag=false;
                }
            }
        },10000)

    },
    methods:{
        toggle_city:function(){//点击切换城市
            // alert(this.city_chow);
            // alert(this.county_chow);
            if(this.city_chow==false){
                this.city_chow=true;
            }
            else if(this.city_chow==true){
                this.city_chow=false;
                this.county_chow=false;
            }
        },
        errImg:function(e){//图片错误处理
            var img= e.target;
            $( e.target).attr("src","../assets/img/imgee.png");
        },
        get_this_city:function(){//定位获取城市，有延时...
            var that=this;
            var timer=setInterval(function(){
                if(cityname!=''&&district_s!=''){
                    //alert('ppp')
                    clearInterval(timer);
                    that.city=cityname;//城市
                    that.cityLon=lon;
                    that.cityLat=lat;
                    that.district=district_s;//区
                }
            },500)
        },
        get_data:function(province,city){//获取酒店列表
            var params = {};
            //params.page = this.currentPage;
            //params.rows = 6;
            params.page=this.page;
            params.pageSize=15;
            params.groupCode=grpCode;//集团编码
            params.cityName = province;//地级市
            params.townName = city;//=区县
            params.token = this.token;//公共token
            params.priceSchemeName="微信会员";//

            //params.token = this.token;
            // params.fileName = imgname;
            // params.fileStream  = this.headerImage;
            var that=this;
            sendRequestPms('/hotel/list', params, function(res) {
                //that.go_login_home(res);
                // goLogin(res);//公用回家
                if(res.status==1){
                    // alert('开始请求')
                    if(res.rows.length!=0){
                        for (var i = 0; res.rows.length>i; i++) {
                            that.cont_list.push(res.rows[i]);
                        }
                    } else{
                        //alert(res.rows.length)
                        if(that.page==1){
                            tips('暂无数据');
                        }
                        that.tipMsg='加载完毕啦!';
                    }
                    //alert('ok')
                    //that.cont_list=res.rows;
                }
            });
        },

        get_city_addre:function(){//把定位到的城市写入标头
            var that=this;
            var params = {};
            params.token = this.token;
            sendRequestPms('/hotel/selectHotelCity',params, function(res) {//请求地级市

                //that.go_login_home(res);
                // goLogin(res);//公用回家
                console.log('okla');
                console.log(res);
                if(res.status == '1') {
                    for(var i=0;i<res.data.length;i++){
                        that.city_aggre.push({
                            name:res.data[i].name,
                            provinceCode:res.data[i].code
                        });
                    }
                } else {
                }
                console.log(8888);
                console.log(that.city_aggre);
            });
        },
        product_load:function(){//下拉执行的具体方法
            this.currentPage++;
            //var that=this;
            //this.get_data(this.provinceCodeId,this.cityCodeId);//把头部显示的标题传进来，按照他们进行查询
            this.get_data(this.city,this.district)
        },
        //go_login_home:function(res){
        //console.log('+++++++');
        //console.log(res);
        //if(res.returnCode=="yuju-1022"){
        ////alert('这里跳转到登录页')
        //window.location.href=backUrl;
        //  }
        //},
        go_cont:function(id,address,name,code,groupId,longitude,latitude,reservePhone,img1Url,img2Url,img3Url,info){
            //alert(id);
            console.log('ppppp');
            //alert(address);
            console.log(id);
            //alert(name);
            _lSSaveOrUpdate( 'hotelId', id ); //http://192.168.1.106:8080/vueModify/weixin/index.html
            _lSSaveOrUpdate( 'address', address ); //保存位置
            _lSSaveOrUpdate( 'hotelName', name ); //保存酒店名
            _lSSaveOrUpdate( 'code', code ); //保存code
            _lSSaveOrUpdate( 'groupId', groupId ); //groupId
            _lSSaveOrUpdate( 'longitude', longitude ); //经度
            _lSSaveOrUpdate( 'latitude', latitude ); //纬度
            _lSSaveOrUpdate( 'reservePhone', reservePhone ); //电话
            _lSSaveOrUpdate( 'info',info); //酒店介绍信息
            _lSSaveOrUpdate( 'hotelImg1Url', img1Url ); //图片路径1
            _lSSaveOrUpdate( 'hotelImg2Url', img2Url ); //图片路径2
            _lSSaveOrUpdate( 'hotelImg3Url', img3Url ); //图片路径3

            if(this.city_chow==false&&this.county_chow==false){
                //router.push({path: '/index-cont', query: {id:this.id}})
                window.location.href=indexUrl+'/app/long-time.html?_t=' + Math.random() ;
            }else{
                this.city_chow=false;
                this.county_chow=false;
            }
        },
        select_city:function(item,index){//点击选择省份，比如北京市
            // console.log('bbbppp');
            this.ctrl = index;
            //alert(this.ctrl);
            //console.log(item);
            this.city_or=item.name;//城市中间件
            //this.provinceCodeId=item.provinceCode;//省份id
            this.county_chow=true;
            if(this.county_aggre.length=!0){
                //alert('000')
                this.county_aggre.length = 0//清空地区数组集合
                //this.count_aggre.splice(0,this.count_aggre.length);
                //console.log('kkkk')
                //console.log(this.county_aggre.length)
            }
            //请求地区
            var that=this;
            var params = {};
            params.cityName = item.name;
            params.token = this.token;//公共token
            // params.token = that.token;
            sendRequestPms('/hotel/selectHotelTown',params, function(res) {//请求城市
                //that.go_login_home(res);//判断跳转
                //goLogin(res);//公用回家
                if(res.status == '1') {
                    //this.county_aggre.push(res.rows[i].cityId);
                    for(var i=0;i<res.data.length;i++){
                        //that.county_aggre.push(res.rows[i].cityId);
                        that.county_aggre.push({
                            name:res.data[i].name,
                            cityCode:res.data[i].cityCode
                        });
                    }
                    // alert('ok')
                } else {
                    //Toast(res.message);
                }
            });
            this.$refs.top_city_box.scrollTop=0;
            //this.city_chow=false;
        },
        distance:function(lon,lat){//计算两点之间的距离
            var map = new BMap.Map("container");
            var point1 = new BMap.Point(this.cityLon,this.cityLat);
            var point2 = new BMap.Point(lon,lat);
            var distances = map.getDistance(point1,point2);
            return (distances/1000).toFixed(1);//米变公里
        },
        select_county:function(ite){//点击选择城市的区，比如朝阳区
            this.city_chow=false;
            this.county_chow=false;
            this.district=ite.name;
            this.cityCodeId=ite.cityCode,//城市id
                this.city=this.city_or;
            this.get_data(this.city,this.district);//第一个参数是中间件的值，第二个是点击的item区县
            //this.currentPage = 1;//先让页变成1
            // this.district=e.target.innerHTML
            this.cont_list.length=0;//再把原来的数组清空
            //this.product_load();//再加载数据
            this.$refs.top_city_box.scrollTop=0;
            this.$refs.top_county_box.scrollTop=0;
        },
    },
    watch:{
        city:function(){
            //alert('bianle')
            var that=this;
            that.witch=2;
            if(that.ctrl_flag==true){
                //alert('0000')
                this.get_city_addre();
                this.get_data(this.city,this.district) ;
                that.ctrl_flag=false;
            }
        }
    },
    computed: {

    },

    components:{

    }






});